//
//  SPStoreList.swift
//  AFNetworking
//
//  Created by admin on 2020/8/31.
//

import SwiftUI
import RealmSwift
import Combine



struct SPStoreList: View {
    
    @ObservedObject var store = UpdateStore()
    
    func delete(index:Int)  {
        let storedata =  self.store.stores[index]
        self.store.stores.remove(at: index)
        StoreData.ug_delete(id: storedata.id)
    }
    
    func getInPrice(pid:String, step:Double)->Double  {
        
        let realm = try! Realm()
        let plans = realm.objects(StoreDataItem.self).filter("parment=='\(pid)'")
        if plans.count == 0 {
            return 0
        }
        var minplan:Double = 9000
        for item in plans {
            
            guard let price = Double(item.price) else { return 0 }
            minplan = minplan < price ? minplan : price
        }
        return minplan*step
    }
    
    var body: some View {
        
        
        NavigationView {
            
            ZStack{
                
                List {
                    ForEach(self.store.stores) { item in
                        NavigationLink(destination: SPStorePlan(pid: item.id, name: item.name) ) {
                            HStack {
                                SPStoreRow(id:item.id,
                                           name: item.name, code: item.code,
                                           inprice: String(format:"%0.3f", arguments:[self.getInPrice(pid:item.id,step: 0.95)]),
                                           outprice: String(format:"%0.3f", arguments:[self.getInPrice(pid:item.id,step: 1.05)]))
                                
                            }
                        }
                        .padding(4)
                        .background(Color("accent"))
                        .cornerRadius(8)
                        
                    }.onDelete { index in
                        self.delete(index: index.first!)
                    }
                    
                }
                
                VStack(alignment: .trailing){
                    Spacer()
                    NavigationLink(destination: SPStorePlanEdit()) {
                        
                        Image(systemName: "plus")
                            .imageScale(.large)
                            .foregroundColor(Color.white)
                            .font(.system(size: 16, weight: .medium))
                            .frame(width: 36, height: 36)
                            .background(Color("danger"))
                            .clipShape(Circle())
                            .shadow(color: Color.black.opacity(0.1), radius: 1, x: 0, y: 1)
                            .shadow(color: Color.black.opacity(0.2), radius: 10, x: 0, y: 10)
                        
                        
                    }
                }
                
            }.navigationBarTitle("Home")
        }
        .navigationViewStyle(StackNavigationViewStyle())
        
    }
}

struct SPStoreList_Previews: PreviewProvider {
    static var previews: some View {
        SPStoreList().environmentObject(UpdateStore())
    }
}
